{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tensorflow import keras\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = keras.datasets.imdb\n",
    "max_word = 10000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "word_index = data.get_word_index()\n",
    "embeddings_index = {}\n",
    "f = open('./glove.6B.100d.txt', encoding=\"utf-8\")\n",
    "for line in f:\n",
    "    values = line.split()\n",
    "    word = values[0]\n",
    "    coefs = np.asarray(values[1:], dtype='float32')\n",
    "    embeddings_index[word] = coefs\n",
    "f.close()\n",
    "embedding_matrix = np.zeros((max_word+1, 100))\n",
    "for word, i in word_index.items():\n",
    "    if i >= max_word:  \n",
    "        continue\n",
    "    embedding_vector = embeddings_index.get(word)  # 根据词向量字典获取该单词对应的词向量\n",
    "    if embedding_vector is not None:\n",
    "        embedding_matrix[i] = embedding_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.37472001, -0.18459   , -0.45389   , -0.72012001,  0.028787  ,\n",
       "       -0.35411   ,  0.57309997, -0.20473   , -0.74261999, -0.76475   ,\n",
       "        0.030966  ,  0.12545   ,  0.26515999,  0.095129  , -0.60201001,\n",
       "       -0.42100999,  0.77305001, -0.65714002, -0.65085   ,  0.13129   ,\n",
       "        0.53500998, -0.37990001, -0.17993   ,  0.12359   ,  1.39460003,\n",
       "        0.85368001, -0.05402   , -0.52389997,  0.27223   , -0.28375   ,\n",
       "       -0.4585    , -0.28073001, -0.71661001,  0.11446   ,  0.32697001,\n",
       "        0.10962   , -0.33671001,  0.20381001,  0.18786   ,  1.15349996,\n",
       "       -0.15603   ,  0.62687999,  0.16644   , -0.56291002, -0.59937   ,\n",
       "       -0.18592   , -0.32602999, -1.01170003, -0.21283001, -0.17839   ,\n",
       "        0.39906999,  0.46171001, -0.93134999, -0.13072   , -0.093857  ,\n",
       "       -1.05879998, -0.50955999,  0.30767   ,  0.66852999,  0.79133999,\n",
       "        0.32890999,  0.13942   ,  0.49458   , -0.14511999,  0.34476   ,\n",
       "        0.035695  ,  1.09930003,  0.71441001, -0.64200997,  0.75129998,\n",
       "       -0.20118   ,  0.40149999, -0.30048999,  0.20597   ,  0.41521999,\n",
       "        0.28839001,  0.54640001, -0.51694   ,  0.50656998, -0.47896001,\n",
       "        0.15718   , -0.045752  , -0.44179001,  0.49562001,  0.25676   ,\n",
       "       -0.30881   , -0.55329001, -0.0086541 , -0.27643999, -0.2886    ,\n",
       "       -0.38799   , -0.48548999,  0.10502   , -0.18561   ,  0.41494   ,\n",
       "        0.43193999,  0.12795   , -0.22171   , -0.24891999,  0.090168  ], dtype=float32)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embedding_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "(x_train,y_train),(x_test,y_test)=data.load_data(num_words=max_word)\n",
    "x_train = keras.preprocessing.sequence.pad_sequences(x_train,300)\n",
    "x_test = keras.preprocessing.sequence.pad_sequences(x_test,300)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用预训练词向量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "model.add(keras.layers.Embedding(max_word,100,weights=[embedding_matrix],input_length=300))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "全连接"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "model = keras.Sequential()\n",
    "model.add(keras.layers.Embedding(max_word,100,input_length=300))\n",
    "model.add(keras.layers.Flatten())\n",
    "model.add(keras.layers.Dense(16,activation='relu'))\n",
    "model.add(keras.layers.Dropout(0.5))\n",
    "model.add(keras.layers.Dense(16,activation='relu'))\n",
    "model.add(keras.layers.Dropout(0.5))\n",
    "model.add(keras.layers.Dense(16,activation='relu'))\n",
    "model.add(keras.layers.Dropout(0.5))\n",
    "model.add(keras.layers.Dense(1,activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "普通CNN"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "model = keras.Sequential()\n",
    "model.add(keras.layers.Embedding(max_word,100,input_length=300))\n",
    "#model.add(keras.layers.Embedding(max_word,100,embeddings_initializer=keras.initializers.Constant(embedding_matrix),input_length=300))\n",
    "model.add(keras.layers.Conv1D(32,3,activation='relu'))\n",
    "model.add(keras.layers.MaxPool1D())\n",
    "model.add(keras.layers.Conv1D(32,4,activation='relu'))\n",
    "model.add(keras.layers.MaxPool1D())\n",
    "model.add(keras.layers.Conv1D(32,5,activation='relu'))\n",
    "model.add(keras.layers.MaxPool1D())\n",
    "model.add(keras.layers.GlobalAveragePooling1D())\n",
    "model.add(keras.layers.Dropout(0.5))\n",
    "model.add(keras.layers.Dense(1,activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LSTM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "model = keras.Sequential()\n",
    "model.add(keras.layers.Embedding(max_word,100,input_length=300))\n",
    "model.add(keras.layers.LSTM(128))\n",
    "model.add(keras.layers.Dropout(0.5))\n",
    "model.add(keras.layers.Dense(1,activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "textCNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "main_input = keras.Input(shape=(300,))\n",
    "embedder = keras.layers.Embedding(max_word+1,100,input_length=300)\n",
    "#embedder = keras.layers.Embedding(max_word,100,embeddings_initializer=keras.initializers.Constant(embedding_matrix),input_length=300,trainable=False)\n",
    "embed = embedder(main_input)\n",
    "cnn1 = keras.layers.Conv1D(32, 3, padding='same', strides=1, activation='relu')(embed)\n",
    "cnn1 = keras.layers.MaxPool1D()(cnn1)\n",
    "cnn2 = keras.layers.Conv1D(32, 4, padding='same',strides=1, activation='relu')(embed)\n",
    "cnn2 = keras.layers.MaxPool1D()(cnn2)\n",
    "cnn3 = keras.layers.Conv1D(32, 5, padding='same',strides=1, activation='relu')(embed)\n",
    "cnn3 = keras.layers.MaxPool1D()(cnn3)\n",
    "cnn = keras.layers.concatenate([cnn1,cnn2,cnn3],axis=-1)\n",
    "cnn = keras.layers.GlobalAveragePooling1D()(cnn)\n",
    "drop =keras.layers.Dropout(0.5)(cnn)\n",
    "#bn = keras.layers.BatchNormalization()(cnn)\n",
    "main_output = keras.layers.Dense(1, activation='sigmoid')(drop)\n",
    "model = keras.Model(inputs=main_input, outputs=main_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "input_1 (InputLayer)            (None, 300)          0                                            \n",
      "__________________________________________________________________________________________________\n",
      "embedding (Embedding)           (None, 300, 100)     1000100     input_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "conv1d (Conv1D)                 (None, 300, 32)      9632        embedding[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "conv1d_1 (Conv1D)               (None, 300, 32)      12832       embedding[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "conv1d_2 (Conv1D)               (None, 300, 32)      16032       embedding[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "max_pooling1d (MaxPooling1D)    (None, 150, 32)      0           conv1d[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "max_pooling1d_1 (MaxPooling1D)  (None, 150, 32)      0           conv1d_1[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "max_pooling1d_2 (MaxPooling1D)  (None, 150, 32)      0           conv1d_2[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "concatenate (Concatenate)       (None, 150, 96)      0           max_pooling1d[0][0]              \n",
      "                                                                 max_pooling1d_1[0][0]            \n",
      "                                                                 max_pooling1d_2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "global_average_pooling1d (Globa (None, 96)           0           concatenate[0][0]                \n",
      "__________________________________________________________________________________________________\n",
      "dropout (Dropout)               (None, 96)           0           global_average_pooling1d[0][0]   \n",
      "__________________________________________________________________________________________________\n",
      "dense (Dense)                   (None, 1)            97          dropout[0][0]                    \n",
      "==================================================================================================\n",
      "Total params: 1,038,693\n",
      "Trainable params: 1,038,693\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['acc'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\Anacona3\\envs\\kr\\lib\\site-packages\\tensorflow\\python\\ops\\gradients_impl.py:112: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.\n",
      "  \"Converting sparse IndexedSlices to a dense Tensor of unknown shape. \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/5\n",
      "25000/25000 [==============================] - 19s 740us/step - loss: 0.5133 - acc: 0.7540 - val_loss: 0.3152 - val_acc: 0.8800\n",
      "Epoch 2/5\n",
      "25000/25000 [==============================] - 11s 438us/step - loss: 0.2803 - acc: 0.8920 - val_loss: 0.2761 - val_acc: 0.8903\n",
      "Epoch 3/5\n",
      "25000/25000 [==============================] - 11s 437us/step - loss: 0.2243 - acc: 0.9148 - val_loss: 0.2742 - val_acc: 0.8887\n",
      "Epoch 4/5\n",
      "25000/25000 [==============================] - 11s 436us/step - loss: 0.1922 - acc: 0.9298 - val_loss: 0.2842 - val_acc: 0.8859\n",
      "Epoch 5/5\n",
      "25000/25000 [==============================] - 11s 437us/step - loss: 0.1680 - acc: 0.9375 - val_loss: 0.3071 - val_acc: 0.8787\n"
     ]
    }
   ],
   "source": [
    "history = model.fit(x_train,y_train,batch_size=128,epochs=5,validation_data=(x_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1dfa8ce4390>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XucVVX9//HXh3G4Ixeh4S5go2haqCNYmtdU1K8SWqZZ\nGZlkqV/9Vl6ynwiSSXkpTb8SKkmWoj2AxEuaafa1b32VQUEuiowgwnAXFbkJw3x+f6w9zmHmMOcM\nnDP7XN7Px+M85py91z7nczaH9dlr7b3XMndHRESkVdwBiIhIblBCEBERQAlBREQiSggiIgIoIYiI\nSEQJQUREACUEERGJKCGIiAighCAiIpF94g6gObp37+4DBgyIOwwRkbwye/bs9e7eI1W5vEoIAwYM\noLKyMu4wRETyipktS6ecuoxERARQQhARkYgSgoiIAEoIIiISUUIQERFACUFERCJKCCIiAuTZfQgi\nIgXPHd5/H1as2PXx7W/DoEFZ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<matplotlib.figure.Figure at 0x1dfa8ce4518>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(history.epoch,history.history.get('acc'),c=\"r\",label=\"acc\")\n",
    "plt.plot(history.epoch,history.history.get('val_acc'),c=\"b\",label=\"val_acc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25000/25000 [==============================] - 4s 142us/step\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.30709582168579103, 0.87871999999999995]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(x_test,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查找字典中词汇对应数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "keras.utils.plot_model(model=model,show_shapes=True,show_layer_names=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_try = np.zeros((1,300))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.empty(300); \n",
    "a.fill(37)\n",
    "x_try[0] = a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.99479574]], dtype=float32)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(x_try)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:kr]",
   "language": "python",
   "name": "conda-env-kr-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
